package com.boodaba.todocarts.ui;

import roboguice.activity.GuiceActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import com.boodaba.todocarts.app.IStartupActivity;
import com.boodaba.todocarts.app.StartupPresenter;
import com.google.inject.Inject;

public class StartupActivity extends GuiceActivity implements IStartupActivity {
	@Inject protected StartupPresenter mPresenter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPresenter.initializeWith(this);
		new Thread(new Runnable() {
			public void run() {
				mPresenter.checkAndUpdate();
				mPresenter.allDone();
			}
		}).start();
	}
	
	public int getCurrentVersion() {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		return prefs.getInt("curver", -1);
	}

	public int getDeclaredVersion() {
		int dVersion = -1;
		try {
			dVersion = getPackageManager()
			.getPackageInfo(getPackageName(), 0)
			.versionCode;
		} catch (NameNotFoundException e) {}
		return dVersion;
	}

	public void setCurrentVersion(int versionCode) {
		SharedPreferences.Editor prefsEdit = PreferenceManager
		.getDefaultSharedPreferences(this)
		.edit();
		prefsEdit.putInt("curver", versionCode);
		prefsEdit.commit();
	}

	public void startMainUi() {
		Intent i = new Intent(this, LabelActivity.class);
		startActivity(i);
	}

	public void exit() {
		finish();
	}

}
